/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.customer.record.service;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.wrapper.customer.record.CustomerRecordModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户记录业务服务
 * 专门处理客户记录相关的业务逻辑，避免循环依赖
 *
 * @author guagua
 * @date 2025-01-04
 */
@Slf4j
@Service
public class CustomerRecordBusinessService {

    @Autowired
    private ICustomerRecordService customerRecordService;

    /**
     * 创建客户新增记录
     *
     * @param customerId 客户ID
     * @return 是否成功
     */
    public boolean createCustomerAddRecord(String customerId) {
        try {
            CustomerRecordModel record = new CustomerRecordModel();
            record.setCustomerId(customerId);
            record.setType("3");

            // 记录详细信息
            Map<String, Object> details = new HashMap<>();
            details.put("类型", "系统新增客户");
            record.setDetails(JSONUtil.toJsonStr(details));
            customerRecordService.insert(record);
            return true;
        } catch (Exception e) {
            log.error("创建客户新增记录失败", e);
            return false;
        }
    }

    /**
     * 创建客户修改记录
     *
     * @param customerId 客户ID
     * @param oldData    旧数据
     * @param newData    新数据
     * @return 是否成功
     */
    public boolean createCustomerUpdateRecord(String customerId, Object oldData, Object newData) {
        try {
            CustomerRecordModel record = new CustomerRecordModel();
            record.setCustomerId(customerId);
            record.setType("3");
            // 比较变更内容
            String changes = compareObjects(oldData, newData);
            record.setDescription("客户信息修改：");

            // 记录详细信息
            Map<String, Object> details = new HashMap<>();
            details.put("oldData", oldData);
            details.put("newData", newData);
            details.put("changes", changes);
            details.put("operationTime", new Date());
            record.setDetails(JSONUtil.toJsonStr(details));

            customerRecordService.insert(record);
            return true;
        } catch (Exception e) {
            log.error("创建客户修改记录失败", e);
            return false;
        }
    }

    /**
     * 创建客户状态变更记录
     *
     * @param customerId 客户ID
     * @param oldStatus  旧状态
     * @param newStatus  新状态
     * @param reason     变更原因
     * @return 是否成功
     */
    public boolean createCustomerStatusChangeRecord(String customerId, String oldStatus, String newStatus,
                                                    String reason) {
        try {
            CustomerRecordModel record = new CustomerRecordModel();
            record.setCustomerId(customerId);
            record.setType("4");

            record.setDescription("客户状态变更");

            // 记录详细信息
            Map<String, Object> details = new HashMap<>();
            details.put("旧状态", oldStatus);
            details.put("新状态", newStatus);
            record.setDetails(JSONUtil.toJsonStr(details));
            customerRecordService.insert(record);
            return true;
        } catch (Exception e) {
            log.error("创建客户状态变更记录失败", e);
            return false;
        }
    }

    /**
     * 比较两个对象的差异
     *
     * @param oldData 旧数据
     * @param newData 新数据
     * @return 差异描述
     */
    private String compareObjects(Object oldData, Object newData) {
        try {
            if (oldData == null && newData == null) {
                return "无变更";
            }
            if (oldData == null) {
                return "新增数据";
            }
            if (newData == null) {
                return "删除数据";
            }

            // 简单的JSON比较
            String oldJson = JSONUtil.toJsonStr(oldData);
            String newJson = JSONUtil.toJsonStr(newData);

            if (oldJson.equals(newJson)) {
                return "无变更";
            }

            return "数据已修改";
        } catch (Exception e) {
            log.warn("比较对象差异失败", e);
            return "数据已修改";
        }
    }
}